package com.example.demo.controller;

import com.example.demo.Response;
import com.example.demo.dto.MessageDTO;
import com.example.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
public class MessageController {
    @Autowired
    private MessageService messageService;
    @DeleteMapping("/message/{messageId}")
    public void deleteMessageByMessageId(@PathVariable long messageId)
    {
        messageService.deleteMessageByMessageId(messageId);
    }
    @GetMapping("/message/{userId}/{receiverId}")
    public Response<Set<MessageDTO>>  getMessageByUserIdAndReceiverId(@PathVariable long userId, @PathVariable long receiverId)
    {
        Set<MessageDTO> messageDTOSet = messageService.getMessageBySenderIdAndReceiverId(userId,receiverId);
        return Response.newSuccess(messageDTOSet);
    }
    @PostMapping("/message/{userId}/{receiverId}")
    public Response<Long> sendMessageBySenderIdAndReceiverId(@RequestBody MessageDTO messageDTO, @PathVariable long userId, @PathVariable long receiverId)
    {
        Long messageId = messageService.sendMessageByUserIdAndReceiverId(messageDTO,userId,receiverId);
        return Response.newSuccess(messageId);
    }
    @GetMapping("/message/latest/{userId}")
    public  Response<Set<MessageDTO>> getLatestMessagesBySenderId(@PathVariable long userId){
        return Response.newSuccess(messageService.getLatestMessagesBySenderId(userId));
    }
}
